package data;

import java.util.ArrayList;
import beans.*;

/*
 * Author: Minh-Tuan Ta
 * Date: 11/6/11
 * Class: MessageDB
 * This class is the data access class for messages
 */
public class MessageDB extends ContentDB {
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getMessages
	 * This method will get all the messages in the database
	 */
	public static synchronized ArrayList<Message> getMessages()
	{
		return convertContentsToMessages(getAllContentOfContenttype(ContentTypeDB.getContentTypeID("Message")));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getMessagesOfUser
	 * This method will get all the messages of a specific user in the database
	 */
	public static synchronized ArrayList<Message> getMessagesOfUser(int userid)
	{
		return convertContentsToMessages(getContentsOfUserAndType(userid, ContentTypeDB.getContentTypeID("Message")));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getMessagesToUser
	 * This method will get all the messages sent to a specific user in the database
	 */
	public static synchronized ArrayList<Message> getMessagesToUser(int userid)
	{
		return convertContentsToMessages(getContentOfExtraAndTypeAndStatus(userid, ContentTypeDB.getContentTypeID("Message"), "status % 7 != 0"));
	}
	
	public static synchronized ArrayList<Message> getUnreadMessagesToUser(int userid)
	{
		return convertContentsToMessages(getContentOfExtraAndTypeAndStatus(userid, ContentTypeDB.getContentTypeID("Message"), "status % 3 = 0"));
	}
	
	public static synchronized ArrayList<Message> getSentMessages(int userid)
	{
		return convertContentsToMessages(getContentOfUserAndTypeAndStatus(userid, ContentTypeDB.getContentTypeID("Message"), "status % 2 != 0"));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getMessage
	 * This method will get a message from the database giving the specific message id
	 */
	public static synchronized Message getMessage(int messageid)
	{
		return convertContentToMessage(getContent(messageid));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: addPost
	 * This method will add a new message
	 */
	public static synchronized int addMessage(Message message)
	{
		return addContent(message);
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: deleteMessage
	 * This method will delete a post
	 */
	public static synchronized int deleteMessage(Message message)
	{
		return deleteContent(message);
	}
}
